package cn.lijian.goods.cart.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.lijian.goods.book.domain.Book;
import cn.lijian.goods.cart.domain.CartItem;
import cn.lijian.goods.cart.service.CartItemService;
import cn.lijian.goods.user.domain.User;

public class CartItemServlet extends BaseServlet {

	private CartItemService cartItemService=new CartItemService();
	/**
	 * 加载多个 cartitem
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String loadCartItems(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cartItemIds=request.getParameter("cartItemIds");
		List<CartItem>cartItemList=cartItemService.loadCartItem(cartItemIds);
		double total=Double.parseDouble(request.getParameter("total"));
		request.setAttribute("cartItemList", cartItemList);
		request.setAttribute("total", total);
		request.setAttribute("cartItemIds", cartItemIds);
		return "f:/jsps/cart/showitem.jsp";
	}
	
	public String updateQuantity(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cartItemId=request.getParameter("cartItemId");
		int quantity=Integer.parseInt(request.getParameter("quantity"));
		CartItem cartItem=cartItemService.updateQuantity(cartItemId, quantity);
		//给客户端返回一个 json
		StringBuilder sb=new StringBuilder("{");
		sb.append("\"quantity\"").append(":").append(cartItem.getQuantity());
		sb.append(",");
		sb.append("\"subtotal\"").append(":").append(cartItem.getSubtotal());
		sb.append("}");
		
		response.getWriter().print(sb.toString());
		return null;
	}
	
	/**
	 * 批量删除 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String batchDelete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cartItemIds=request.getParameter("cartItemIds");
		cartItemService.batchDelete(cartItemIds);
		return myCart(request, response);
	}
	/**
	 * 添加条目
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 * @author lijian
	 */
	public String add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Map map=request.getParameterMap();
		CartItem cartItem=CommonUtils.toBean(map, CartItem.class);
		Book book=CommonUtils.toBean(map, Book.class);
		User user=(User)request.getSession().getAttribute("sessionUser");
		cartItem.setBook(book);
		cartItem.setUser(user);
		try {
			cartItemService.add(cartItem);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		return myCart(request, response);
		
	}
	public String myCart(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		User user=(User) request.getSession().getAttribute("sessionUser");
		String uid=user.getUid();
		List<CartItem>cartItemList=cartItemService.myCart(uid);
		request.setAttribute("cartItemList", cartItemList);
		return "f:/jsps/cart/list.jsp";
	}

}
